/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import javax.validation.Valid;

import entity.Customer;
import service.CustomerService;

/**
 *
 * @author cyhiew
 */
public class CustomerControllerTest extends SimpleFormController {

    private CustomerService customerService;

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    public CustomerControllerTest() {
        //Initialize controller properties here or 
        //in the Web Application Context

        setCommandClass(Customer.class);
        setCommandName("customer");
        setSuccessView("customerView");
        setFormView("customerInputView");
    }

    //Use onSubmit instead of doSubmitAction 
    //when you need access to the Request, Response, or BindException objects
    @Override
    protected ModelAndView onSubmit(@Valid
            HttpServletRequest request,
            HttpServletResponse response,
            Object command,
            BindException errors) throws Exception {
        Customer name = (Customer) command;
        ModelAndView mv = new ModelAndView(getSuccessView());
        //mv.addObject("helloMessage", helloService.sayHello(name.getValue()));
        System.out.println("QWE " + name.getCustomerId());
        mv.addObject("customerList", customerService.getCustomerList());
        mv.addObject("message", "QWERTY");
        return mv;
    }
}